package cn.tx.doubaodemo.common.result;

import lombok.Getter;

import java.io.Serializable;

/**
 * @author tx
 * @date 2022/8/26 17:01
 */
@Getter
public class IResult<T> implements Serializable {

    private final int code;

    private final String message;

    private T data;

    // ******************** private constructors ********************
    private IResult(String message) {
        this.code = 200;
        this.message = message;
    }

    private IResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    private IResult(T data) {
        this.data = data;
        this.code = 200;
        this.message = "success";
    }

    // ******************** common result template ********************

    public static <T> IResult<T> success(T data) {
        return new IResult<>(data);
    }

    public static <T> IResult<T> success(String message) {
        return new IResult<>(message);
    }

    public static <T> IResult<T> fail(ResultCodeEnum resultCode) {
        return new IResult<>(resultCode.getCode(), resultCode.getMessage());
    }

    public static <T> IResult<T> fail(int code, String message) {
        return new IResult<>(code, message);
    }

    public static <T> IResult<T> fail(String message) {
        return new IResult<>(500, message);
    }
}
